package com.example.demo.util;

import org.json.JSONArray;
import org.json.JSONObject;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Classname Md5Util
 * @Description TODO
 * @Date 2019/9/3 16:33
 * @Created by yby
 */
public class Md5UtilTest {


    public static void main(String args[]) throws NoSuchAlgorithmException {

        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        System.out.println("uuid="+uuid);
        String kk = "981c3f6b8e104b129ebec89a0c68d3f0";
        System.out.println(kk.length());
       /* Map<String,Object> map=new HashMap<>();
        map.put("companyName","ddd");
        map.put("merchantType","xxx");
        map.put("region","yyy");
        String companyName ="";
        String merchantType ="";
        String region ="";
        for (String key : map.keySet()) {

             companyName = (String) map.get("companyName");
              merchantType = (String) map.get("merchantType");
              region = (String) map.get("region");

        }
        System.out.println("companyName="+companyName);
        System.out.println("merchantType="+merchantType);
        System.out.println("region="+region);*/

        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("buyType", 1);
        jsonObject.put("buyNumber", 1);
        //jsonObject.put("ticketType", "10元");
        jsonObject.put("size", 1);
        jsonArray.put(jsonObject);

        jsonObject = new JSONObject();
        jsonObject.put("buyType", 2);
        jsonObject.put("buyNumber", 1);
        //jsonObject.put("ticketType", "10元");
        jsonObject.put("size", 1);
        jsonArray.put(jsonObject);

        JSONObject json = new JSONObject();
        json.put("cpOdd", "mqttTest2");
        json.put("orderId", "1");
        json.put("result", jsonArray);
        json.put("time",System.currentTimeMillis());
        //json.put("accesskey",selectConnectConfig.getAccesskey());
        //json.put("secretkey","123456");
        //String md5Str = "cpOdd=mqttTest2&orderId=1&result="+jsonArray.toString()+"&time=1567577468327&secretkey=02f7e697217940d08a48aa0be96962ee";
        String md5Str = "cpOdd=mqttTest2&orderId=1&result=[{\"buyNumber\":1,\"size\":1,\"buyType\":1},{\"buyNumber\":1,\"size\":1,\"buyType\":2}]&time=1567577968559&secretkey=02f7e697217940d08a48aa0be96962ee";
        //String md5Str = "cpOdd=mqttTest2&orderId=1&result="+jsonArray.toString()+"time="+System.currentTimeMillis()+"secretkey="+selectConnectConfig.getSecretkey();
        md5Str= "orderId=1&successBooyNumber=1&successWelfareNumber=1&type=1&time=1567567073523";
        md5Str= "orderId=1&successBooyNumber=1&successWelfareNumber=1&type=1&time=1567567073523";
        System.out.println("json="+json.toString());
        System.out.println("md5Str="+md5Str);
        //json.put("sign",new Md5Util().getMD5Encode(json.toString()));
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(md5Str.getBytes());
        json.put("sign",bytes2Hex(md5.digest()));
        String str = json.toString();
        System.out.println("str="+str);
        String s = "partnerid=171&accesskey=83dc18c7bf0e37fda2559a5f2f0e28eb&secretkey=901a2004ef7903627fdc6a2b8016f164&version=1.0&stamp=1556085017";

        //s = "123456";
        /*MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(s.getBytes());
        System.out.println(bytes2Hex(md5.digest()));*/

        System.out.println("System.currentTimeMillis()="+System.currentTimeMillis());
        System.out.println("System.currentTimeMillis()="+String.valueOf(System.currentTimeMillis()));
    }

    private static String bytes2Hex(byte[] bts) {
        String des = "";
        String tmp = null;

        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }
}
